import {
  Shader,
  ShaderData,
  ShaderProperty,
  ShaderPool,
  ShaderProgram,
  ShaderProgramPool,
  ShaderMacro,
  ShaderMacroCollection,
  ShaderDataGroup,
  ShaderLib
} from "@nirvana/shaders";

import { WebGLCanvas } from "@nirvana/canvas";

// 使用使用 Canvas
let canvas = document.getElementById("engine-nirvana");
// console.log(canvas);

const webCanvas = new WebGLCanvas(canvas, {
  // fullScreen: true, // 自动放大全屏
  // fixWidth: 500, // 放大
  // auto: true // 页面设置了canvas 样式
  // 2d: true,
});
// webCanvas.fullScreen();
const engine = webCanvas.engine;
const gl = engine.gl;
const pool1: ShaderProgramPool = new ShaderProgramPool();

const planeVS = `
attribute vec4 aVertexPosition;
void main() {
  gl_Position = aVertexPosition;
}
`;
const planeFS = `
void main() {
  gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
`;

const plane = Shader.create("plane", planeVS, planeFS);
console.log(plane, Shader.find("plane"));
const pool3: ShaderProgramPool = new ShaderProgramPool();
const shaderMacroCollection: ShaderMacroCollection = new ShaderMacroCollection();
console.log(shaderMacroCollection);
const planeProgram: ShaderProgram = plane.getShaderProgram(engine.gl, shaderMacroCollection, pool3);
console.log("plane", planeProgram);

const aVertexPosition = planeProgram.attributeLocation.aVertexPosition;

const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);

var vertices = [-0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5];

gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);

const planeShaderData: ShaderData = new ShaderData(ShaderDataGroup.Material);
console.log(ShaderDataGroup, planeShaderData);

engine.update = (gl) => {
  // console.log('--');
  gl.clearColor(0.0, 0.0, 0.0, 1.0);
  // 用上面指定的颜色清除缓冲区
  gl.clear(gl.COLOR_BUFFER_BIT);
  // 面start 二
  planeProgram.bind();
  gl.vertexAttribPointer(aVertexPosition, 2, gl.FLOAT, false, 0, 0);
  gl.enableVertexAttribArray(aVertexPosition);
  gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
  // 面end
};
